package com.skp.shaphan.clipboard;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.util.ArrayList;

public class RtfSelection implements Transferable {
    private static ArrayList<DataFlavor> rtfFlavors = new ArrayList<DataFlavor>();
    static {
        try {
        	rtfFlavors.add(new DataFlavor("application/rtf; class=java.io.InputStream"));
            rtfFlavors.add(new DataFlavor("text/rtf;class=java.lang.String"));
            rtfFlavors.add(new DataFlavor("text/rtf;class=java.io.Reader"));
            rtfFlavors.add(new DataFlavor("text/rtf;charset=unicode;class=java.io.InputStream"));
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    
    private String rtf;
    public RtfSelection(String rtf) {
    	this.rtf = rtf;
    }

	@Override
	public Object getTransferData(DataFlavor flavor)
			throws UnsupportedFlavorException, IOException {
        if (String.class.equals(flavor.getRepresentationClass())) {
            return rtf;
        } else if (Reader.class.equals(flavor.getRepresentationClass())) {
            return new StringReader(rtf);
        } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
            return new StringBufferInputStream(rtf);
        }
        throw new UnsupportedFlavorException(flavor);
	}

	@Override
	public DataFlavor[] getTransferDataFlavors() {
		return rtfFlavors.toArray(new DataFlavor[0]);
	}

	@Override
	public boolean isDataFlavorSupported(DataFlavor flavor) {
		return rtfFlavors.contains(flavor);
	}

}
